package com.pipayshop.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.pipayshop.entity.UserByZone;
import com.pipayshop.entity.UserInfo;
import com.pipayshop.entity.vo.ResponseVO;
import com.pipayshop.exception.BusinessException;
import com.pipayshop.mapper.UserInfoMapper;
import com.pipayshop.service.UserByZoneService;
import com.pipayshop.service.ZoneLeaderConfigurationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;


/**
 * <p>
 * 用户专区信息controller
 * </p>
 *
 * @author c_shunyi
 * @since 2023-11-21
 */
@Api(tags = "用户专区信息")
@RestController
@RequestMapping("/pipayshopapi/UserByZone")
public class UserByZoneController {

    @Autowired
    UserByZoneService userByZoneService;
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    ZoneLeaderConfigurationService zoneLeaderConfigurationService;

    @ApiOperation("添加用户专区信息")
    @PostMapping("addUserByZone")
    public ResponseVO<String> addUserByZone(@RequestBody Map<String, Object> requestBody) {
        String userId = requestBody.get("userId").toString();
        if (userId.isEmpty())
            throw new BusinessException("user_not_logged_in");
        if (userInfoMapper.selectOne(new QueryWrapper<UserInfo>()
                .eq("uid", userId)) == null)
            throw new BusinessException("user_info_not_found");
        if (userByZoneService.getOne(new QueryWrapper<UserByZone>()
                .eq("user_id", userId)) != null)
            throw new BusinessException("user_zone_info_exists");
        try {
            UserByZone userByZone = new UserByZone();
            userByZone.setUserId(userId);
            userByZone.setUserThreshold(zoneLeaderConfigurationService.getById(1).getThresholdSum());
            userByZoneService.save(userByZone);
            return ResponseVO.getSuccessResponseMsg(null, "用户专区信息添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("add_user_zone_info_failed");
        }

    }

    @ApiOperation("根据用户ID查询用户专区信息")
    @GetMapping("selectUserZoneByUid/{userId}")
    public ResponseVO<UserByZone> SelectUserByZone(@PathVariable String userId) {
        if (StringUtils.isEmpty(userId)) {
            throw new BusinessException("invalid_parameter");
        }
        return ResponseVO.getSuccessResponseVo(userByZoneService.getOne(new QueryWrapper<UserByZone>()
                .eq("user_id", userId)));
    }

    @ApiOperation("升级为团长")
    @PostMapping("zoneLeader")
    public ResponseVO<String> zoneLeader(@RequestBody Map<String, Object> requestBody) {
        String userId = requestBody.get("userId").toString();
        if (StringUtils.isEmpty(userId)) {
            throw new BusinessException("invalid_parameter");
        }
        BigDecimal thresholdSum = zoneLeaderConfigurationService.getById(1).getThresholdSum();
        if (userByZoneService.getOne(new QueryWrapper<UserByZone>()
                .eq("user_id", userId)).getZoneConsumptionSum().compareTo(thresholdSum) < 0) {
            throw new BusinessException("spending_did_not_meet_threshold");
        }
        if (userByZoneService.getOne(new QueryWrapper<UserByZone>()
                .eq("user_id", userId)
                .eq("is_group_leader", 1)) != null) {
            throw new BusinessException("already_a_leader");
        }
        try {
            userByZoneService.update(null, new UpdateWrapper<UserByZone>()
                    .eq("user_id", userId)
                    .set("is_group_leader", 1)
                    .set("opening_group_qualification", 1));
            return ResponseVO.getSuccessResponseMsg(null, "升级为团长成功");
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException("leader_upgrade_failed");
        }
    }

}
